from django.shortcuts import render, HttpResponse, redirect
from app01 import models
# Create your views here.


USER_DICT = {
    '1': {'name': 'root1', 'ip': '127.0.0.1', 'port': '80'},
    '2': {'name': 'root2', 'ip': '127.0.0.1', 'port': '80'},
    '3': {'name': 'root3', 'ip': '127.0.0.1', 'port': '80'},
    '4': {'name': 'root4', 'ip': '127.0.0.1', 'port': '80'},
    '5': {'name': 'root5', 'ip': '127.0.0.1', 'port': '80'}
}


def index(request):
    return render(request, 'index.html', {'user_dict': USER_DICT})

# # URL方法二的函数
# def detail(request, nid):
#     # nid = request.GET.get('nid')
#     return render(request, 'detail.html', {'user_info': USER_DICT[nid]})

# URL方法一的函数
# def detail(request):
#     nid = request.GET.get('nid')
#     return render(request, 'detail.html', {'user_info': USER_DICT[nid]})


def detail(request, uid):
    return render(request, 'detail.html', {'user_info': USER_DICT[uid]})


# def detail(request, *args):
#     uid = args[-1]
#     return render(request, 'detail.html', {'user_info': USER_DICT[uid]})


def login(request):
    if request.method == "POST":
        u = request.POST.get('username')
        p = request.POST.get('pwd')
        if models.UserInfo.objects.filter(username=u, password=p).first():
            # return render(request, 'index_1.html', {'user': u})
            return redirect('/index_1')
        else:
            return render(request, 'login.html')
    return render(request, 'login.html')


def index_1(request):
    return render(request, 'index_1.html')


def user_info(request):
    user_list = models.UserInfo.objects.all()
    return render(request, 'user_info.html', {'user_list': user_list})


def orm(request):
    # models.UserInfo.objects.create(username='eric', password='123')
    # re = models.UserInfo.objects.filter(password='123')
    # for i in re:
    #     print(i.id,i.username,i.password)
    # models.UserInfo.objects.filter(id='3').delete()
    models.UserInfo.objects.filter(username="root").update(password="hhh")
    return HttpResponse('orm')
